package com.um.jdy.ykc.command;

import cn.hutool.core.util.ArrayUtil;
import com.um.jdy.ykc.utils.RadixsUtil;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import lombok.Data;

/**
 * 运营平台远程控制启机（运营平台->充电桩）
 *
 * @author 甘天伟
 * @since 2022/6/28
 * 版权所有 广州优匠科技有限公司
 */
@Data
public class Cmd34ServerStartChargeRequest extends YkcCommand {

    /**
     * 交易流水号：16个字节
     */
    private String transactionNo;

    /**
     * 桩编码：7个字节
     */
    private String code;

    /**
     * 枪号：1个字节
     */
    private int gunNo;

    /**
     * 逻辑卡号：8个字节
     */
    private String cardNo;

    /**
     * 物理卡号：8个字节
     */
    private String physicsNo;

    /**
     * 账户余额：4个字节
     */
    private double balance;

    public Cmd34ServerStartChargeRequest(int serial,String transactionNo,String code,int gunNo,
                                         String cardNo,String physicsNo,double balance) {
        super();

        this.serial = serial;
        this.encFlag = RadixsUtil.hexToInteger("00");
        this.command = RadixsUtil.hexToInteger("34");

        this.transactionNo = transactionNo;
        this.code = code;
        this.gunNo = gunNo;
        this.cardNo = cardNo;
        this.physicsNo = physicsNo;
        this.balance = balance;


        byte[] _transactionNo = RadixsUtil.hexToBytes(transactionNo);
        byte[] _code = RadixsUtil.hexToBytes(code);
        byte _gunNo = (byte)gunNo;
        byte[] _cardNo = RadixsUtil.hexToBytes(cardNo);
        byte[] _physicsNo = RadixsUtil.hexToBytes(physicsNo);
        byte[] _balance = ArrayUtil.reverse(RadixsUtil.double32ToBytes(balance, 2));

        ByteBuf buff = Unpooled.buffer();
        buff.writeBytes(fillZeroBytes(_transactionNo,16 - _transactionNo.length));
        buff.writeBytes(fillZeroBytes(_code,7 - _code.length));
        buff.writeByte(_gunNo);
        buff.writeBytes(fillZeroBytes(_cardNo,8 - _cardNo.length));
        buff.writeBytes(fillZeroBytes(_physicsNo,8 - _physicsNo.length));
        buff.writeBytes(_balance);

        this.data = new byte[buff.readableBytes()];
        buff.readBytes(data);
        buff.release();

        postConstruct();
    }
}
